﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Configuration;
using System.IO;
using Agresso.Interface.Layer.Domain;
using System.Xml;
using Agresso.Interface.Layer.Gateway;
using System.Data.SqlClient;
using Agresso.Interface.Layer.SQL;

namespace Agresso.Interface.Layer.Service
{
    public class MaxPostInvoiceService
    {
        DataBaseConnectivity m_dataBaseConectivity;
        AcrBatcInputService m_AcrBatcInputService;
        ConfigurationSettingService m_ConfigurationSettingService;

        public MaxPostInvoiceService()
        {
            m_dataBaseConectivity = new DataBaseConnectivity();
            m_AcrBatcInputService = new AcrBatcInputService();
            m_ConfigurationSettingService = new ConfigurationSettingService();
        }
        public MaxPostInvoice ReadFormXMLFile(string xmlFile)
        {
            MaxPostInvoice mHeader = null;
            
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(xmlFile);
                XmlNodeList maxPostInvoice = doc.SelectNodes("MaxPostInvoice");
                mHeader = new MaxPostInvoice();
                mHeader.FileName = Path.GetFileName(xmlFile);
                XmlNodeList header = maxPostInvoice[0].SelectNodes("Header");
                mHeader.DocumentType = header[0].SelectNodes("DocumentType") == null ? "" : header[0].SelectNodes("DocumentType")[0].Attributes["value"].Value;
                mHeader.DocumentDate = header[0].SelectNodes("DocumentDate") == null ? new DateTime(1900, 1, 1) : Convert.ToDateTime(header[0].SelectNodes("DocumentDate")[0].Attributes["value"].Value);
                mHeader.PostingDate = header[0].SelectNodes("PostingDate") == null ? new DateTime(1900, 1, 1) : Convert.ToDateTime(header[0].SelectNodes("PostingDate")[0].Attributes["value"].Value);
                mHeader.InvoiceIndicator = header[0].SelectNodes("InvoiceIndicator")[0] == null ? false : Convert.ToBoolean(header[0].SelectNodes("InvoiceIndicator")[0].Attributes["value"].Value);
                mHeader.ObjectType = header[0].SelectNodes("ObjectType")[0] == null ? "" : header[0].SelectNodes("ObjectType")[0].Attributes["value"].Value;
                mHeader.ReferenceDocumentNumber = header[0].SelectNodes("ReferenceDocumentNumber")[0] == null ? "" : header[0].SelectNodes("ReferenceDocumentNumber")[0].Attributes["value"].Value;
                mHeader.CompanyCode = header[0].SelectNodes("CompanyCode")[0] == null ? "" : header[0].SelectNodes("CompanyCode")[0].Attributes["value"].Value;
                mHeader.VendorID = header[0].SelectNodes("VendorID")[0] == null ? "" : header[0].SelectNodes("VendorID")[0].Attributes["value"].Value;
                mHeader.PurchaseOrderNumber = header[0].SelectNodes("PurchaseOrderNumber")[0] == null ? "" : header[0].SelectNodes("PurchaseOrderNumber")[0].Attributes["value"].Value;
                mHeader.Currency = header[0].SelectNodes("Currency")[0] == null ? "" : header[0].SelectNodes("Currency")[0].Attributes["value"].Value;
                mHeader.GrossAmount = Convert.ToDecimal(header[0].SelectNodes("GrossAmount")[0] == null ? "0" : header[0].SelectNodes("GrossAmount")[0].Attributes["value"].Value);
                mHeader.NetAmount = Convert.ToDecimal(header[0].SelectNodes("NetAmount")[0] == null ? "0" : header[0].SelectNodes("NetAmount")[0].Attributes["value"].Value);
                mHeader.BaselineDate = header[0].SelectNodes("BaselineDate")[0] == null ? new DateTime(1900, 1, 1) : Convert.ToDateTime(header[0].SelectNodes("BaselineDate")[0].Attributes["value"].Value);
                mHeader.HeaderText = header[0].SelectNodes("HeaderText")[0] == null ? "" : header[0].SelectNodes("HeaderText")[0].Attributes["value"].Value;
                mHeader.DeliveryCosts = Convert.ToDecimal(header[0].SelectNodes("DeliveryCosts")[0] == null ? "0" : header[0].SelectNodes("DeliveryCosts")[0].Attributes["value"].Value);

                XmlNodeList receiverInformation = maxPostInvoice[0].SelectNodes("ReceiverInformation");
                mHeader.ReceiverName1 = receiverInformation[0].SelectNodes("ReceiverName1")[0] == null ? "" : receiverInformation[0].SelectNodes("ReceiverName1")[0].Attributes["value"].Value;
                mHeader.ReceiverPOCode = receiverInformation[0].SelectNodes("ReceiverPOCode")[0] == null ? "" : receiverInformation[0].SelectNodes("ReceiverPOCode")[0].Attributes["value"].Value;
                mHeader.ReceiverCity = receiverInformation[0].SelectNodes("ReceiverCity")[0] == null ? "" : receiverInformation[0].SelectNodes("ReceiverCity")[0].Attributes["value"].Value;
                mHeader.ReceiverHousenumber = receiverInformation[0].SelectNodes("ReceiverHousenumber")[0].Attributes["value"] == null ? "" : receiverInformation[0].SelectNodes("ReceiverHousenumber")[0].Attributes["value"].Value;

                XmlNodeList senderInformation = maxPostInvoice[0].SelectNodes("SenderInformation");
                mHeader.Name1 = senderInformation[0].SelectNodes("Name1")[0] == null ? "" : senderInformation[0].SelectNodes("Name1")[0].Attributes["value"].Value;
                mHeader.BankAccountNumber = senderInformation[0].SelectNodes("BankAccountNumber")[0] == null ? "" : senderInformation[0].SelectNodes("BankAccountNumber")[0].Attributes["value"].Value;
                mHeader.BankNumber = senderInformation[0].SelectNodes("BankNumber")[0] == null ? "" : senderInformation[0].SelectNodes("BankNumber")[0].Attributes["value"].Value;
                mHeader.TaxNumber1 = senderInformation[0].SelectNodes("TaxNumber1")[0] == null ? "" : senderInformation[0].SelectNodes("TaxNumber1")[0].Attributes["value"].Value;
                mHeader.VatRegistrationNumber = senderInformation[0].SelectNodes("VatRegistrationNumber")[0] == null ? "" : senderInformation[0].SelectNodes("VatRegistrationNumber")[0].Attributes["value"].Value;

                XmlNodeList bankdata = maxPostInvoice[0].SelectNodes("Bankdata");
                XmlNodeList bankdataitem = bankdata[0].SelectNodes("BankdataItem");
                mHeader.BankAccountNumber_ = bankdataitem[0].SelectNodes("BankAccountNumber")[0] == null ? "" : bankdataitem[0].SelectNodes("BankAccountNumber")[0].Attributes["value"].Value;
                mHeader.BankNumber_ = bankdataitem[0].SelectNodes("BankNumber")[0] == null ? "" : bankdataitem[0].SelectNodes("BankNumber")[0].Attributes["value"].Value;

                XmlNodeList taxData = maxPostInvoice[0].SelectNodes("TaxData");
                XmlNodeList taxItem = taxData[0].SelectNodes("TaxItem");
                mHeader.TaxPercentage = Convert.ToDecimal(taxItem[0].SelectNodes("TaxPercentage")[0] == null ? "0" : taxItem[0].SelectNodes("TaxPercentage")[0].Attributes["value"].Value);
                mHeader.TaxAmount = Convert.ToDecimal(taxItem[0].SelectNodes("TaxAmount")[0] == null ? "0" : taxItem[0].SelectNodes("TaxAmount")[0].Attributes["value"].Value);

                XmlNodeList lineItems = maxPostInvoice[0].SelectNodes("LineItems");
                XmlNodeList lineItem = lineItems[0].SelectNodes("LineItem");
                if (lineItem.Count > 0)
                {
                   foreach(XmlNode line in lineItem)
                    {
                        MaxPostInvoice item = new MaxPostInvoice();
                        item.PurchaseOrderNumber_ = line.SelectNodes("PurchaseOrderNumber")[0] == null ? "" : line.SelectNodes("PurchaseOrderNumber")[0].Attributes["value"].Value;
                        item.PurchaseOrderItem = line.SelectNodes("PurchaseOrderItem")[0] == null ? "" : line.SelectNodes("PurchaseOrderItem")[0].Attributes["value"].Value;
                        item.ItemVendorNumber = line.SelectNodes("ItemVendorNumber")[0] == null ? "" : line.SelectNodes("ItemVendorNumber")[0].Attributes["value"].Value;
                        item.ItemText = line.SelectNodes("ItemText")[0] == null ? "" : line.SelectNodes("ItemText")[0].Attributes["value"].Value;
                        item.ItemAmount = Convert.ToDecimal(line.SelectNodes("ItemAmount")[0] == null ? "0" : line.SelectNodes("ItemAmount")[0].Attributes["value"].Value);
                        item.ItemDiscount = Convert.ToDecimal(line.SelectNodes("ItemDiscount")[0] == null ? "0" : line.SelectNodes("ItemDiscount")[0].Attributes["value"].Value);
                        item.Quantity = Convert.ToDecimal(line.SelectNodes("Quantity")[0] == null ? "0" : line.SelectNodes("Quantity")[0].Attributes["value"].Value);
                        mHeader.AddItem(item);
                    }
                }

            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            return mHeader;
 
        }
        public void ImportDataFromXml()
        {
            try
            {
                string maxPostInvoiceFileFolder = ConfigurationManager.AppSettings["MaxPostInvoiceFileFolder"];
                string maxPostInvoiceBackUpFileFolder = ConfigurationManager.AppSettings["MaxPostInvoiceBackUpFileFolder"];
                string[] files = System.IO.Directory.GetFiles(maxPostInvoiceFileFolder);
                foreach (string file in files)
                {
                    string ext = Path.GetExtension(file);
                    if (ext.ToLower() != ".xml") continue;
                    MaxPostInvoice result = null;

                    DeleteLogFile(file + "_log.log");
                    try
                    {
                        result = ReadFormXMLFile(file);
                    }
                    catch (Exception x)
                    {
                        CreateLogFile(file + "_log.log", DateTime.Now + " : " + x.Message);
                    }
                    if (result != null)
                    {
                        InsertToDB(result);
                        string destinationFile = maxPostInvoiceBackUpFileFolder + file.Substring(file.LastIndexOf("\\") + 1);
                        File.Move(file, AssertFileExist(maxPostInvoiceBackUpFileFolder, destinationFile));
                    }
                }
            }
            catch (Exception x)
            {
                throw x;
            }
        }
        private void InsertToDB(MaxPostInvoice result)
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(MaxPostInvoiceSQL.InsertMaxPostInvoice(), m_dataBaseConectivity.InterfaceSqlConnection);
                if (result.LineItems.Count == 0)
                    result.LineItems.Add(result);

                foreach(MaxPostInvoice item in result.LineItems)
                {
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@DocumentType", System.Data.SqlDbType.NVarChar).Value = item.DocumentType;
                    cmd.Parameters.Add("@DocumentDate", System.Data.SqlDbType.DateTime).Value = item.DocumentDate;
                    cmd.Parameters.Add("@PostingDate", System.Data.SqlDbType.DateTime).Value = item.PostingDate;
                    cmd.Parameters.Add("@InvoiceIndicator", System.Data.SqlDbType.Bit).Value = item.InvoiceIndicator;
                    cmd.Parameters.Add("@ObjectType", System.Data.SqlDbType.NVarChar).Value = item.ObjectType;
                    cmd.Parameters.Add("@ReferenceDocumentNumber", System.Data.SqlDbType.NVarChar).Value = item.ReferenceDocumentNumber;
                    cmd.Parameters.Add("@CompanyCode", System.Data.SqlDbType.NVarChar).Value = item.CompanyCode;
                    cmd.Parameters.Add("@VendorID", System.Data.SqlDbType.NVarChar).Value = item.VendorID;
                    cmd.Parameters.Add("@PurchaseOrderNumber", System.Data.SqlDbType.NVarChar).Value = item.PurchaseOrderNumber;
                    cmd.Parameters.Add("@Currency", System.Data.SqlDbType.NVarChar).Value = item.Currency;
                    cmd.Parameters.Add("@GrossAmount", System.Data.SqlDbType.Decimal).Value = item.GrossAmount;
                    cmd.Parameters.Add("@NetAmount", System.Data.SqlDbType.Decimal).Value = item.NetAmount;
                    cmd.Parameters.Add("@BaselineDate", System.Data.SqlDbType.DateTime).Value = item.BaselineDate;
                    cmd.Parameters.Add("@HeaderText", System.Data.SqlDbType.NVarChar).Value = item.HeaderText;
                    cmd.Parameters.Add("@DeliveryCosts", System.Data.SqlDbType.Decimal).Value = item.DeliveryCosts;
                    cmd.Parameters.Add("@ReceiverName1", System.Data.SqlDbType.NVarChar).Value = item.ReceiverName1;
                    cmd.Parameters.Add("@ReceiverPOCode", System.Data.SqlDbType.NVarChar).Value = item.ReceiverPOCode;
                    cmd.Parameters.Add("@ReceiverCity", System.Data.SqlDbType.NVarChar).Value = item.ReceiverCity;
                    cmd.Parameters.Add("@ReceiverHousenumber", System.Data.SqlDbType.NVarChar).Value = item.ReceiverHousenumber;
                    cmd.Parameters.Add("@Name1", System.Data.SqlDbType.NVarChar).Value = item.Name1;
                    cmd.Parameters.Add("@BankAccountNumber", System.Data.SqlDbType.NVarChar).Value = item.BankAccountNumber;
                    cmd.Parameters.Add("@BankNumber", System.Data.SqlDbType.NVarChar).Value = item.BankNumber;
                    cmd.Parameters.Add("@TaxNumber1", System.Data.SqlDbType.NVarChar).Value = item.TaxNumber1;
                    cmd.Parameters.Add("@VatRegistrationNumber", System.Data.SqlDbType.NVarChar).Value = item.VatRegistrationNumber;
                    cmd.Parameters.Add("@BankAccountNumber_", System.Data.SqlDbType.NVarChar).Value = item.BankAccountNumber_;
                    cmd.Parameters.Add("@BankNumber_", System.Data.SqlDbType.NVarChar).Value = item.BankNumber_;
                    cmd.Parameters.Add("@TaxPercentage", System.Data.SqlDbType.Decimal).Value = item.TaxPercentage;
                    cmd.Parameters.Add("@TaxAmount", System.Data.SqlDbType.Decimal).Value = item.TaxAmount;
                    cmd.Parameters.Add("@PurchaseOrderNumber_", System.Data.SqlDbType.NVarChar).Value = item.PurchaseOrderNumber_;
                    cmd.Parameters.Add("@PurchaseOrderItem", System.Data.SqlDbType.NVarChar).Value = item.PurchaseOrderItem;
                    cmd.Parameters.Add("@ItemAmount", System.Data.SqlDbType.Decimal).Value = item.ItemAmount;
                    cmd.Parameters.Add("@ItemDiscount", System.Data.SqlDbType.Decimal).Value = item.ItemDiscount;
                    cmd.Parameters.Add("@Quantity", System.Data.SqlDbType.Decimal).Value = item.Quantity;
                    cmd.Parameters.Add("@ItemVendorNumber", System.Data.SqlDbType.NVarChar).Value = item.ItemVendorNumber;
                    cmd.Parameters.Add("@ItemText", System.Data.SqlDbType.NVarChar).Value = item.ItemText;
                    cmd.Parameters.Add("@FileName", System.Data.SqlDbType.NVarChar).Value = item.FileName;
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }
        public void CreateLogFile(string fileName, string log)
        {
            StreamWriter file = new StreamWriter(fileName);
            file.WriteLine(log);
            file.Close();
        }
        public void DeleteLogFile(string fileName)
        {
            if (File.Exists(fileName))
                File.Delete(fileName);
        }
        public static string AssertFileExist(string destinationFolder, string file)
        {
            if (File.Exists(file))
            {
                string fileName = file.Substring(file.LastIndexOf("\\") + 1);
                string[] files = fileName.Split('.');
                string file1 = files[0];
                string file2 = files[1];
                file = destinationFolder + file1 + "_Copy." + file2;
                file = AssertFileExist(destinationFolder, file);
                return file;
            }
            return file;
        }
        private int GetRunningNumber()
        {
            ConfigurationSetting runningNumber = m_ConfigurationSettingService.GetConfigurationSetting("RunningNumber");
            int number = Convert.ToInt32(runningNumber.val_1) + 1;
            runningNumber.val_1 = number.ToString();
            m_ConfigurationSettingService.UpdateConfiguration(runningNumber);
            return number;
        }
        public void ExecuteInterface()
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(MaxPostInvoiceSQL.SelectAllPendingInfoice(), m_dataBaseConectivity.InterfaceSqlConnection);
                SqlDataReader rd = cmd.ExecuteReader();
                IList invoiceResult = new ArrayList();
                IList result = new ArrayList();
                while (rd.Read())
                    invoiceResult.Add(rd[0].ToString());
                rd.Close();
                cmd.CommandText = MaxPostInvoiceSQL.SelectAllPendingInfoiceByFilename();
                foreach (string inv in invoiceResult)
                {
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@FileName", System.Data.SqlDbType.NVarChar).Value = inv;
                    rd = cmd.ExecuteReader();
                    MaxPostInvoice header = null;
                    while (rd.Read())
                    {
                        if (header == null)
                            header = new MaxPostInvoice(rd);
                        header.AddItem(new MaxPostInvoice(rd));
                    }
                    result.Add(header);
                    rd.Close();
                }
                int batchIDCount = 0;
                foreach (MaxPostInvoice ki in result)
                {
                    IList acrbatchinputList = new ArrayList();
                    int voucherNumber = GetRunningNumber();
                    batchIDCount++;
                    string batch_id = "BI" + DateTime.Now.ToString("ddMMyyyyHHmm") + batchIDCount.ToString().PadLeft(2, '0');
                    acrbatchinput acrbatchinput_AP = new acrbatchinput();
                    acrbatchinput_AP.voucher_type = "RI";
                    acrbatchinput_AP.trans_type = "AP";
                    acrbatchinput_AP.client = "PT";
                    acrbatchinput_AP.apar_type = "P";
                    acrbatchinput_AP.apar_id = ki.VendorID;
                    acrbatchinput_AP.apar_name = ki.ReceiverName1;
                    acrbatchinput_AP.account = "3000";
                    acrbatchinput_AP.dim_1 = "";
                    acrbatchinput_AP.dim_2 = "";
                    acrbatchinput_AP.dim_3 = "";
                    acrbatchinput_AP.dim_4 = "";
                    acrbatchinput_AP.dim_5 = "";
                    acrbatchinput_AP.dim_6 = "";
                    acrbatchinput_AP.dim_7 = "";
                    acrbatchinput_AP.tax_code = "0";
                    acrbatchinput_AP.tax_system = "";
                    acrbatchinput_AP.currency = ki.Currency;
                    acrbatchinput_AP.dc_flag = 0;
                    acrbatchinput_AP.cur_amount = 0;
                    acrbatchinput_AP.amount = ki.NetAmount;
                    acrbatchinput_AP.value_1 = 0;
                    acrbatchinput_AP.value_2 = 0;
                    acrbatchinput_AP.value_3 = 0;
                    acrbatchinput_AP.description = "";
                    acrbatchinput_AP.trans_date = ki.DocumentDate;
                    acrbatchinput_AP.voucher_date = ki.DocumentDate;
                    acrbatchinput_AP.voucher_no = voucherNumber;
                    acrbatchinput_AP.ext_inv_ref = ki.ReferenceDocumentNumber;
                    acrbatchinput_AP.status = "N";
                    //acrbatchinput_AP.due_date = pda.TransactionDate;
                    acrbatchinput_AP.period = Convert.ToInt32(ki.PostingDate.ToString("yyyyMM"));
                    acrbatchinput_AP.responsible = "DEV01";
                    acrbatchinput_AP.batch_id = batch_id;
                    acrbatchinput_AP.interface_ = "BI";
                    //acrbatchinput_AP.disc_date = pda.TransactionDate;
                    acrbatchinput_AP.sequence_no = GetRunningNumber();
                    acrbatchinputList.Add(acrbatchinput_AP);

                    foreach (MaxPostInvoice item in ki.LineItems)
                    {
                        acrbatchinput acrbatchinput_GL = new acrbatchinput();
                        acrbatchinput_GL.voucher_type = "RI";
                        acrbatchinput_GL.trans_type = "GL";
                        acrbatchinput_GL.client = "PT";
                        acrbatchinput_GL.apar_type = "";
                        acrbatchinput_GL.apar_id = "";
                        acrbatchinput_GL.apar_name = "";
                        acrbatchinput_GL.account = "";
                        acrbatchinput_GL.dim_1 = "";
                        acrbatchinput_GL.dim_2 = "";
                        acrbatchinput_GL.dim_3 = "";
                        acrbatchinput_GL.dim_4 = "";
                        acrbatchinput_GL.dim_5 = "";
                        acrbatchinput_GL.dim_6 = "";
                        acrbatchinput_GL.dim_7 = "";
                        acrbatchinput_GL.tax_code = "0";
                        acrbatchinput_GL.tax_system = "";
                        acrbatchinput_GL.currency = "";
                        acrbatchinput_GL.dc_flag = 0;
                        acrbatchinput_GL.cur_amount = -item.ItemAmount;
                        acrbatchinput_GL.amount = 0;
                        acrbatchinput_GL.value_1 = 0;
                        acrbatchinput_GL.value_2 = 0;
                        acrbatchinput_GL.value_3 = 0;
                        acrbatchinput_GL.description = ki.ItemText;
                        acrbatchinput_GL.trans_date = ki.DocumentDate;
                        acrbatchinput_GL.voucher_date = ki.DocumentDate;
                        acrbatchinput_GL.voucher_no = voucherNumber;
                        acrbatchinput_GL.ext_inv_ref = ki.ReferenceDocumentNumber;
                        acrbatchinput_GL.status = "N";
                        //acrbatchinput_GL.due_date = pda.TransactionDate;
                        acrbatchinput_GL.period = 201404;// Convert.ToInt32(DateTime.Today.ToString("yyyyMM"));
                        acrbatchinput_GL.responsible = "DEV01";
                        acrbatchinput_GL.batch_id = batch_id;
                        acrbatchinput_GL.interface_ = "BI";
                        // acrbatchinput_GL.disc_date = pda.TransactionDate;
                        acrbatchinput_GL.sequence_no = GetRunningNumber();
                        acrbatchinputList.Add(acrbatchinput_GL);
                    }
                    //insert ACRBATCHinput
                    // string agrtid = "";
                    foreach (acrbatchinput acr in acrbatchinputList)
                    {
                        //m_AcrBatcInputService.Insert(acr);
                        m_AcrBatcInputService.InsertToInterfaceTable(acr);
                        //agrtid += ";" + acr.agrtid.ToString();
                    }
                    //update status-------
                    // if (agrtid != "")
                    // {
                    foreach (MaxPostInvoice item in ki.LineItems)
                    {
                        UpdateStatus("",1, ((acrbatchinput)acrbatchinputList[0]).batch_id, item.i_maxpostinvoice_id);
                    }
                    // }
                }
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }
        private void UpdateStatus(string remark, int status, string batch_id, int id)
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection_b.Open();
                SqlCommand cmd = new SqlCommand(MaxPostInvoiceSQL.UpdateMaxPostInvoiceStatus(), m_dataBaseConectivity.InterfaceSqlConnection_b);
                cmd.Parameters.Add("@batch_id", System.Data.SqlDbType.NVarChar).Value = batch_id;
                cmd.Parameters.Add("@remark", System.Data.SqlDbType.NVarChar).Value = remark;
                cmd.Parameters.Add("@pickup_flag", System.Data.SqlDbType.Int).Value = status;
                cmd.Parameters.Add("@i_maxpostinvoice_id", System.Data.SqlDbType.Int).Value = id;
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection_b.Close();
            }
        }
    }
}
